package com.mxpio.erp.oa.service.impl;

import com.mxpio.erp.common.enums.OaEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.oa.entity.OaReimbursementSpecial;
import com.mxpio.erp.common.oa.entity.OaReimbursementSpecialLine;
import com.mxpio.erp.common.oa.service.OaReimbursementSpecialService;
import com.mxpioframework.camunda.service.BpmnFlowService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.security.entity.Dept;
import com.mxpioframework.security.service.DeptService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class OaReimbursementSpecialServiceImpl extends BaseServiceImpl<OaReimbursementSpecial> implements OaReimbursementSpecialService {

    @Autowired
    private BpmnFlowService bpmnFlowService;

    @Autowired
    private DeptService deptService;
    @Override
    @Transactional
    public Result<OaReimbursementSpecial> saveall(OaReimbursementSpecial oaReimbursementSpecial) {
        oaReimbursementSpecial.setOrderStatus(OaEnums.OrderStatus.NEW.getCode());
        oaReimbursementSpecial.setBpmnStatus(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode());

        String applyUserDeptId = oaReimbursementSpecial.getApplyUserDeptId();
        Dept levelOneDept = deptService.getLevelOneDept(applyUserDeptId);
        if (levelOneDept != null) {
            oaReimbursementSpecial.setOrgDesc(StringUtils.isNotBlank(levelOneDept.getDeptDesc())?levelOneDept.getDeptDesc():levelOneDept.getDeptName());
        }

        this.save(oaReimbursementSpecial, new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context){
                Object o = context.getEntity();
                if (o instanceof OaReimbursementSpecialLine){
                    OaReimbursementSpecial info = context.getParent();
                    ((OaReimbursementSpecialLine) o).setBizNo(info.getBizNo());
                }
                return  true;
            }
        });
        return Result.OK(oaReimbursementSpecial);
    }

    @Override
    @Transactional
    public Result<?> submit(String key, String loginUsername, String businessKey, Map<String, Object> properties) {
        String bizNo = properties.get("bizNo").toString();
        OaReimbursementSpecial byId = this.getById(OaReimbursementSpecial.class, bizNo);
        if (byId == null){
            return Result.error("单据不存在");
        }
        if ( byId.getOrderStatus().equals(OaEnums.OrderStatus.UNDER_REVIEW.getCode())
                || byId.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())){
            return Result.error("单据正在审核中，请勿重复提交");
        }
        if (byId.getOrderStatus().equals(OaEnums.OrderStatus.CLOSED.getCode())||byId.getOrderStatus().equals(OaEnums.OrderStatus.DONE.getCode())){
            return Result.error("单据已关闭或已完成，请勿重复提交");
        }
        if (!byId.getOrderStatus().equals(OaEnums.OrderStatus.NEW.getCode())){
            return Result.error("单据新增状态是才能提交审核");
        }
        byId.setOrderStatus(OaEnums.OrderStatus.UNDER_REVIEW.getCode());
        byId.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode());
        this.update(byId);

        //发起工作流
        ProcessInstance processInstance = bpmnFlowService.startWithFormByKey(key, loginUsername, businessKey, properties);
        byId.setProcessInstanceId(processInstance.getId());
        this.update(byId);
        return Result.OK(byId);
    }

    @Override
    @Transactional
    public Result<?> submitOK(OaReimbursementSpecial reimbursement) {
        String bizNo = reimbursement.getBizNo();
        OaReimbursementSpecial byId = this.getById(OaReimbursementSpecial.class, bizNo);
        if (byId == null){
            return Result.error("单据不存在");
        }
        //撤销
        if(StringUtils.equals(reimbursement.getRevoke(),"1")){
            byId.setOrderStatus(OaEnums.OrderStatus.NEW.getCode());
            byId.setBpmnStatus(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode());
            this.update(byId);
            return Result.OK(byId);
        }
        //审核通过
        if (reimbursement.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())){
            byId.setOrderStatus(OaEnums.OrderStatus.DONE.getCode());
            byId.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
        }
        //审核不通过
        if (reimbursement.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CLOSED.getStatusCode())){
            byId.setOrderStatus(OaEnums.OrderStatus.CLOSED.getCode());
            byId.setBpmnStatus(OrderStatusEnums.BpmnStatus.CLOSED.getStatusCode());
        }
        this.update(byId);
        return Result.OK(byId);
    }
}
